<?php

    include 'database.php';
    
    // Variables
    $appname        = $_GET['appname'];
    $appversion     = $_GET['appversion'];
    $deviceuid      = $_GET['deviceuid'];
    $devicetoken    = $_GET['devicetoken'];
    $devicename     = $_GET['devicename'];
    $devicemodel    = $_GET['devicemodel'];
    $deviceversion  = $_GET['deviceversion'];
    
    if (strlen($appname) == 0) {
        exit(0);
    }

    $con = mysql_connect($hostname,$username,$password) or die('Could not connect to database : ' . mysql_error());

    mysql_select_db($database);
    
    //echo "<br>Connected to database " . $database;
    
    $done = false;
		    
/*---------------------------------------------------------------------------------------------------------------*/    
/*---------------------------------------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------------*/
    
    if ( ! $done ) {
    
		$query = "SELECT * FROM $devicestable WHERE devicetoken='$devicetoken'"; 

		$result = mysql_query($query) or die('Error executing query :<br>'.$query.'<br>'.mysql_error()); 

		while($data = mysql_fetch_assoc($result)) 
		{
			$query = "UPDATE $devicestable SET appname='$appname', appversion='$appversion', deviceuid='$deviceuid', devicename='$devicename', devicemodel='$devicemodel', deviceversion='$deviceversion', updated=now() WHERE devicetoken='$devicetoken'";
			//echo "<br>Query to execute : $query";
			mysql_query($query) or die('Error executing query :<br>'.$query.'<br>'.mysql_error()); 
			$done = true;
			break;
		}

	}
    
/*---------------------------------------------------------------------------------------------------------------*/    
/*---------------------------------------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------------*/
    
    if ( ! $done ) {
    
		$query = "SELECT * FROM $devicestable WHERE deviceuid='$deviceuid'"; 

		$result = mysql_query($query) or die('Error executing query :<br>'.$query.'<br>'.mysql_error()); 

		while($data = mysql_fetch_assoc($result)) 
		{
			$query = "UPDATE $devicestable SET appname='$appname', appversion='$appversion', devicetoken='$devicetoken', devicename='$devicename', devicemodel='$devicemodel', deviceversion='$deviceversion', updated=now() WHERE deviceuid='$deviceuid'";
			//echo "<br>Query to execute : $query";
			mysql_query($query) or die('Error executing query :<br>'.$query.'<br>'.mysql_error()); 
			$done = true;
			break;
		}
    
    }
    
/*---------------------------------------------------------------------------------------------------------------*/    
/*---------------------------------------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------------*/

		if ( ! $done )
		{
			$query = "INSERT INTO $devicestable (appname, appversion, deviceuid, devicetoken, devicename, devicemodel, deviceversion) VALUES ('$appname', '$appversion', '$deviceuid', '$devicetoken', '$devicename', '$devicemodel', '$deviceversion')";
			//echo "<br>Query to execute : $query";
			mysql_query($query) or die('Error executing query :<br>'.$query.'<br>'.mysql_error()); 
			$done = true;
		}

    mysql_close($con);
    
?>